package com.jetpackframework.virtualapk;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.jetpackframework.VirtualInterface;

public class VirtualServiceConnection implements ServiceConnection {
    private static VirtualServiceConnection serviceConnection;
    private String apkPath;

    public VirtualInterface getVirtualInterface() {
        return virtualInterface;
    }

    private VirtualInterface virtualInterface;
    public static synchronized VirtualServiceConnection getInstance() {
        if (serviceConnection == null){
            serviceConnection = new VirtualServiceConnection();
        }
        return serviceConnection;
    }

    public void loadApk(String apkPath) throws RemoteException {
        virtualInterface.loadApk(apkPath);
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        virtualInterface = VirtualInterface.Stub.asInterface(service);
        try {
            loadApk(apkPath);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        virtualInterface = null;

    }

    public void setApkPath(String apkPath) {
        this.apkPath = apkPath;
    }
}
